//
//  LDSPersonalViewController.m
//  TestCategory
//
//  Created by kai_leedarson on 2017/10/19.
//Copyright © 2017年 maple_leedarson. All rights reserved.
//

#import "LDSPersonalViewController.h"
#import "LDSPersonalViewControllerViewModel.h"
#import "LDSTableViewModel.h"
#import "LDSCustomBtn.h"
#import "UIResponder+Router.h"
#import "LDSPickView.h"
#import "UIView+TYAlertView.h"

@interface LDSPersonalViewController ()

@property(nonatomic, strong) LDSPersonalViewControllerViewModel *vcViewModel;//必须子类化
@property(nonatomic, strong) UITableView *tableView;
@property(nonatomic, strong) UIView *headView;
@property(nonatomic, strong) LDSTableViewModel *tableViewModel;//选择性子类化
@property(nonatomic, strong) NSMutableArray *dataSource;
@property(nonatomic, strong) LDSCustomBtn *bottomBtn;
@property(nonatomic, strong) UIImageView *avatar;
@property (strong, nonatomic) UIImagePickerController *imagePickerController;
@end

@implementation LDSPersonalViewController

#pragma mark - lazy init
- (LDSPersonalViewControllerViewModel *)vcViewModel {
    if (_vcViewModel == nil) {
        _vcViewModel = [[LDSPersonalViewControllerViewModel alloc] init];
        _vcViewModel.delegate = (id<LDSViewControllerViewModelDelegate>)self;
    }
    return _vcViewModel;
}

- (NSMutableArray *)dataSource {
    if (_dataSource == nil) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds  style:UITableViewStyleGrouped];
        _tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
        _tableView.tableHeaderView = self.headView;
    }
    return _tableView;
}

- (UIView *)headView {
    if (_headView == nil) {
        _headView = [[UIView alloc] init];
        CGRect frame = self.view.frame;
        frame.size.height = 180;
        _headView.frame = frame;
        UIImageView *avatar = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Login_icon_head"]];
        avatar.center = _headView.center;
        [_headView addSubview:avatar];
        self.avatar = avatar;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(avatarTap:)];
        [avatar addGestureRecognizer:tap];
        avatar.userInteractionEnabled = YES;
        
        UILabel *avatarLabel = [[UILabel alloc] init];
        avatarLabel.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width - 160)/2,CGRectGetMaxY(avatar.frame),160,40);
        avatarLabel.text = @"Modify Photos";
        avatarLabel.textColor = [UIColor lightGrayColor];
        avatarLabel.textAlignment = NSTextAlignmentCenter;
        [_headView addSubview:avatarLabel];
    }
    return _headView;
}

- (LDSCustomBtn *)bottomBtn {
    if (_bottomBtn == nil) {
        _bottomBtn = [[LDSCustomBtn alloc] init];
        [_bottomBtn setTitle:@"Done" forState:UIControlStateNormal];
    }
    return _bottomBtn;
}

- (LDSTableViewModel *)tableViewModel {
    if (_tableViewModel == nil) {
        _tableViewModel = [[LDSTableViewModel alloc] init];
        _tableViewModel.delegate = (id<LDSTableViewModelDelegate>)self;
        //tableViewModel接管tableView
        [_tableViewModel handleWithTable:self.tableView];
    }
    return _tableViewModel;
}

- (UIImagePickerController *)imagePickerController{
    if (!_imagePickerController) {
        _imagePickerController =[[UIImagePickerController alloc]init];
        _imagePickerController.modalPresentationStyle = UIModalTransitionStyleCoverVertical;
        _imagePickerController.allowsEditing = YES;
        [_vcViewModel handleWithImagePickerController:_imagePickerController];
    }
    return _imagePickerController;
}

- (NSString *)barItemTitle {
    return @"Personal Info";
}

#pragma mark - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    //tableViewModel接管dataSource 数据源
    [self.tableViewModel handleWithDataSource:self.dataSource];
    [self.view addSubview:self.tableView];
    [self.vcViewModel loadFetchData];
    [self.view addSubview:self.bottomBtn];
}

#pragma mark - LDSTableViewModelDelegate
- (void)lds_tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}

#pragma mark - LDSViewControllerViewModelDelegate
- (void)lds_didReceiveDataWithDataSource:(NSArray *)dataSource andSectionDataSource:(NSArray *)sectionDataSource {
    self.dataSource = [dataSource mutableCopy];
    [self.tableViewModel handleWithDataSource:self.dataSource];
    [self.tableView reloadData];
}

#pragma mark - Router Manager
- (void)routerEventWithName:(NSString *)eventName userInfo:(NSDictionary *)userInfo {
    NSLog(@"%@",userInfo);
    if ([eventName isEqualToString:@"Age"]) {
        UITableViewCell *cell = userInfo[@"cell"];
        [self showPickView:cell.tag];
    } else if ([eventName isEqualToString:@"Gender"]) {
        LDSBaseTableViewCell *cell = userInfo[@"cell"];
        [self showAlertViewGender:cell];
    } else if ([eventName isEqualToString:@"pickView"]) {
        NSInteger row = [userInfo[@"row"] integerValue];
        LDSBaseTableViewCell *cell =
        (LDSBaseTableViewCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0]];
        [self setCell:cell andDetail:userInfo[@"detail"]];
    } else if ([eventName isEqualToString:@"avatar"]) {
        UIImage *image = userInfo[@"image"];
        self.avatar.image = image;
        self.avatar.layer.cornerRadius = self.avatar.frame.size.height/2;
        self.avatar.layer.masksToBounds = YES;
    }
}

#pragma mark - Gesture
- (void)avatarTap:(UIImageView *)sender {
    [self showAlertPhotos];
}

#pragma mark - privite
-(void)showPickerController:(NSInteger)index {
    if (index == 0) {
        if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            return;
        }
        self.imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
    } else if (index ==1){
        self.imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }
    [self presentViewController:self.imagePickerController animated:YES completion:nil];
}

- (void)showPickView:(NSInteger)tag {
    LDSPickView *settingAgeView = [LDSPickView createViewFromNib];
    settingAgeView.pickView.tag = tag;
    [self.vcViewModel handleWithPickView:settingAgeView.pickView];// 接管pickView 的代理
    TYAlertController *alertController = [TYAlertController alertControllerWithAlertView:settingAgeView preferredStyle:TYAlertControllerStyleActionSheet];
    alertController.backgoundTapDismissEnable = YES;
    [self presentViewController:alertController animated:YES completion:nil];
}

- (void)showAlertViewGender:(LDSBaseTableViewCell *)cell {
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"Your Gender"
                                                                    message:nil
                                                             preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction * Male = [UIAlertAction actionWithTitle:@"Male"
                                                    style:UIAlertActionStyleDefault
                                                  handler:^(UIAlertAction * _Nonnull action) {
                                                      [self setCell:cell andDetail:@"Male"];
                                                  }];
    UIAlertAction * Female = [UIAlertAction actionWithTitle:@"Female"
                                                      style:UIAlertActionStyleDefault
                                                    handler:^(UIAlertAction * _Nonnull action) {
                                                         [self setCell:cell andDetail:@"Female"];
                                                    }];
    UIAlertAction * Secret = [UIAlertAction actionWithTitle:@"Secret"
                                                      style:UIAlertActionStyleDefault
                                                    handler:^(UIAlertAction * _Nonnull action) {
                                                        [self setCell:cell andDetail:@"Secret"];
                                                    }];
    UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"cancel"
                                                      style:UIAlertActionStyleCancel
                                                    handler:^(UIAlertAction * _Nonnull action) {
                                                    }];
    [alert addAction:Male];
    [alert addAction:Female];
    [alert addAction:Secret];
    [alert addAction:cancel];
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)showAlertPhotos {
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:nil
                                                                   message:nil
                                                            preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction * takePhoto = [UIAlertAction actionWithTitle:@"Take Photo"
                                                        style:UIAlertActionStyleDefault
                                                      handler:^(UIAlertAction * _Nonnull action) {
                                                          [self showPickerController:0];
                                                      }];
    UIAlertAction * Album = [UIAlertAction actionWithTitle:@"Choose from Album"
                                                    style:UIAlertActionStyleDefault
                                                  handler:^(UIAlertAction * _Nonnull action) {
                                                      [self showPickerController:1];
                                                  }];
    UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"Cancel"
                                                     style:UIAlertActionStyleCancel
                                                   handler:nil];
    [alert addAction:takePhoto];
    [alert addAction:Album];
    [alert addAction:cancel];
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)setCell:(LDSBaseTableViewCell *)cell andDetail:(NSString *)detail {
    LDSPersonalModel *model = (LDSPersonalModel *)cell.model;
    model.detailInfo = detail;
    [cell setModel:model];
}
@end
